package com.lhr.common.support;

import java.util.Collection;
import java.util.Map;

import org.apache.commons.lang.builder.ToStringStyle;

public class JSONToStringStyle extends ToStringStyle {
	private static final long serialVersionUID = 1L;

	JSONToStringStyle() {
		super();
		this.setUseClassName(false);
		this.setUseIdentityHashCode(false);
		this.setContentStart("{");
		this.setContentEnd("}");
		this.setArrayStart("[");
		this.setArrayEnd("]");
		this.setFieldNameValueSeparator(":");
		this.setNullText("\"\"");

	}

	@Override
	protected void appendFieldStart(StringBuffer buffer,
			String fieldName) {
		if (this.isUseFieldNames() && fieldName != null) {
			buffer.append("\"");
			buffer.append(fieldName);
			buffer.append("\"");
			buffer.append(this.getFieldNameValueSeparator());
		}
	}

	@Override
	protected void appendInternal(StringBuffer buffer,
			String fieldName,
			Object value,
			boolean detail) {
		if ((value.getClass().isArray() || value instanceof Map || value instanceof Collection) == false) {
			buffer.append("\"");
		}
		super.appendInternal(buffer, fieldName, value, detail);
		if ((value.getClass().isArray() || value instanceof Map || value instanceof Collection) == false) {
			buffer.append("\"");
		}
	}

}
